package com.own.component.util.locate.util;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * LocateDistanceFormatter
 *
 * @author chenxueli
 * @date 2023-08-01 08:40:00
 */
public class LocateDistanceFormatter {

    /**
     * 距离为0时的格式化字符串
     */
    public static final String DISTANCE_ZERO = "0.0m";

    /**
     * 格式化距离
     *
     * @param distance 距离（m）
     * @return 格式化后的距离
     */
    public static String format(BigDecimal distance) {
        if (distance == null) {
            return "0m";
        }
        distance = distance.abs();
        if (distance.compareTo(new BigDecimal(800)) >= 0) {
            return distance.divide(new BigDecimal(1000), 1, RoundingMode.HALF_UP) + "km";
        }
        return distance.setScale(0, RoundingMode.HALF_UP) + "m";
    }

}
